package cn.ac.ict.fpevaluation.evaluation;

import cn.ac.ict.fpevaluation.bean.APBean;
import cn.ac.ict.fpevaluation.bean.AtomTrajectoryBean;
import cn.ac.ict.fpevaluation.bean.StepBean;
import cn.ac.ict.fpevaluation.fastdtw.dtw.TimeWarpInfo;
import cn.ac.ict.fpevaluation.fastdtw.dtw.WarpPath;
import cn.ac.ict.fpevaluation.fastdtw.test.FastDTWTest;
import cn.ac.ict.fpevaluation.fastdtw.test.MagneticSample;
import cn.ac.ict.fpevaluation.fastdtw.timeseries.TimeSeriesPoint;
import cn.ac.ict.fpevaluation.utils.WifiHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuyouxiong on 2017/3/7.
 */
public class WifiEvaluationByMagnetic {

    public void remedyWifiByMagnetic(AtomTrajectoryBean traj1, AtomTrajectoryBean traj2) {
        ArrayList<MagneticSample> mag1 = traj2MagneticList(traj1);
        ArrayList<MagneticSample> mag2 = traj2MagneticList(traj2);
        TimeWarpInfo t = new FastDTWTest().calDistance(gettsArray(mag1), gettsArray(mag2), 3);
        WarpPath path = t.getPath();
        for (int i = 0; i < mag1.size(); i++) {
            ArrayList<Object> matchingJ = path.getMatchingIndexesForI(i);
            int stepCountForI = getStepCountByMag(i, mag1);
            List<APBean> apForI = traj1.getAps().get(stepCountForI);
            List<List<APBean>> apForJs = new ArrayList<>();
            for (Object j : matchingJ) {
                List<APBean> apBeans = traj2.getAps().get(getStepCountByMag((int) (j), mag2));
                apForJs.add(apBeans);
            }

            remedyRss(apForI, apForJs);
        }


    }

    private int getStepCountByMag(int i, ArrayList<MagneticSample> samples) {
        return samples.get(i).getStepCount();
    }

    private void remedyRss(List<APBean> apForI, List<List<APBean>> apForJs) {
        apForJs.forEach(item -> WifiHelper.remedyRssByMac(apForI, item));
    }

    protected ArrayList<MagneticSample> traj2MagneticList(AtomTrajectoryBean data) {
        // 把AtomTrajectoryBean变成Sample集合
        ArrayList<MagneticSample> sampleLists = new ArrayList<>();
        int stepCount = 0;
        for (StepBean step : data.getStepList()) {
            int size = step.getxMagnetic().size();
            for (int j = 0; j < size; j++) {
                MagneticSample sample = new MagneticSample();
                sample.setMagXOrigin(step.getxMagnetic().get(j));
                sample.setMagYOrigin(step.getyMagnetic().get(j));
                sample.setMagZOrigin(step.getzMagnetic().get(j));
                sample.setStepCount(stepCount);
                sampleLists.add(sample);
            }
            stepCount++;
        }

        return sampleLists;
    }

    private static ArrayList<Object> gettsArray(ArrayList<?> samplelist) {
        ArrayList<Object> tsArray = new ArrayList<Object>();

        for (int i = 0; i < samplelist.size(); i++) {
            final ArrayList<Double> currentLineValues = new ArrayList<Double>();
            Object sample = samplelist.get(i);
            if (sample instanceof MagneticSample) {
                currentLineValues.add(((MagneticSample) sample).getMagXOrigin());
                currentLineValues.add(((MagneticSample) sample).getMagYOrigin());
                currentLineValues.add(((MagneticSample) sample).getMagZOrigin());
            }

            final TimeSeriesPoint readings = new TimeSeriesPoint(currentLineValues.subList(0, currentLineValues.size()));

            tsArray.add(readings);
        }
        return tsArray;
    }

}
